#include "StdAfx.h"
#include "Logger.h"
#include <sstream>
#include "Picture.h"

Logger::Logger()
	: mCurrentLogMessage(_T("Please load BMP picture"))
{}

std::wstring Logger::GetLogText() const
{
	std::wostringstream s;
	if (mLogData.empty()) s << "Data was not gathered yet.";
	else
	{
		s << "Time in milliseconds for various tasks:\n\n";
		LogInfo prevInfo = {0, 0};
		DWORD firstTime;
		for (LogData::const_iterator i = mLogData.begin();
			i != mLogData.end(); ++i)
		{
			if (0 != prevInfo.text)
			{
				s << (i->time - prevInfo.time) << " - " << 
					prevInfo.text << std::endl;
			}
			else
			{
				firstTime = i->time;
			}
			prevInfo = *i;
		}
		s << "\nTotal time: " << prevInfo.time - firstTime << 
			std::endl << std::endl << prevInfo.text;
	}
	return s.str();
}

void Logger::Log(const TCHAR* inText)
{
	LogInfo info;
	info.time = ::GetTickCount();
	info.text = inText;
	mLogData.push_back(info);
	mCurrentLogMessage = inText;
}

const std::wstring& Logger::GetStatusText() const
{
	mLastShownIsLog = true;
	return mCurrentLogMessage;
}

void Logger::Clear()
{
	mLogData.clear();
}

void Logger::SetCurrentLogMessage(const std::wstring& msg)
{
	mCurrentLogMessage = msg;
}

std::wstring Logger::PixelPositionStr(int inX, int inY, const Picture& pic) const
{
	int width = pic.GetRawWidth();
	int height = pic.GetRawHeight();
	if (0 == width || 0 == height)
	{
		return std::wstring();
	}
	float scale = pic.GetScale();
	float shiftX, shiftY;
	pic.GetDotShift(shiftX, shiftY);

	float x = inX / scale - shiftX - 0.5f; 
	float y = height - 1 - inY / scale - shiftY + 0.5f;

	if (inX >= width * scale || inX < 0 || inY < 0 || inY >= height * scale)
	{
		return mLastShownIsLog ? _T("") : _T(" ");
	}
	
	mLastShownIsLog = false;
	std::wostringstream s;
	s.setf(std::ios_base::fixed);
	s.precision(2);
	s << "x=" << x << " y=" << y;
	std::wstring dotsInfo = pic.GetSketch().GetHighlightedInfo();
	if (!dotsInfo.empty())
	{
		s << "; " << dotsInfo;
	}

	return s.str();
}